#import "dfhgwhruebvhrheysternideology.h"
#import "dfhgwhruebvhrheyluggagerecorder.h"
#import "dfhgwhruebvhrheythrivealternate.h"
#import "dfhgwhruebvhrheytragedymotivateViewController.h"
#import "dfhgwhruebvhrheyindependenceyearlyViewController.h"
@interface dfhgwhruebvhrheytragedymotivateViewController ()
@end
@implementation dfhgwhruebvhrheytragedymotivateViewController {
    UIView *bgView;
    UILabel *phoneText;
    UIButton *continueBtn;
    dfhgwhruebvhrheythrivealternate *phone;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputTextChange:) name:UITextFieldTextDidChangeNotification object:nil];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadUI];
}
- (void)loadUI {
    [self configBackIcon:@"dhgfywegwygeydwcurvereciprocal"];
    self.navigationItem.title = @"Forgot password";
    self.navigationStyle = dhgfywegwygeydwreciprocaltransport;
    bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    phoneText = [[UILabel alloc] init];
    phoneText.text = @"Phone number";
    phoneText.font = [UIFont regular:16];
    phoneText.textColor = text_color1.dhgfywegwygeydwlearneddesignate;
    [bgView addSubview:phoneText];
    phone = [[dfhgwhruebvhrheythrivealternate alloc] init];
    [bgView addSubview:phone];
    phone.left_text = @"+63";
    phone.rightBtn = nil;
    phone.canEdit = !self.canNotEditMobile;
    phone.placeholder = @"Please Enter Phone Number";
    if (self.mobile) {
        phone.content = self.mobile;
    }
    continueBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    continueBtn.tag = 101;
    continueBtn.layer.cornerRadius = 10;
    continueBtn.titleLabel.font = [UIFont medium:16];
    continueBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
    [continueBtn setTitle:@"Continue" forState:UIControlStateNormal];
    [continueBtn setTitleColor:white_color.dhgfywegwygeydwlearneddesignate forState:UIControlStateNormal];
    [continueBtn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:continueBtn];
}
- (void)inputTextChange:(NSNotification *)not {
    UITextField *input = [not object];
    if ([input.text hasPrefix:@"8"] || [input.text hasPrefix:@"9"]) {
        if (input.text.length >= 10) {
            phone.content = [input.text substringToIndex:10];
            [self.view endEditing:YES];
        }else {
            phone.content = input.text;
        }
    }else {
        if (input.text.length >= 11) {
            phone.content = [input.text substringToIndex:11];
            [self.view endEditing:YES];
        }else {
            phone.content = input.text;
        }
    }
}
- (void)btnAction {
    [self.view endEditing:YES];
    if (phone.content.length == 0) {
        [self.view show:@"Mobile number cannot be empty"];
        return;
    }
    if (![phone.content dhgfywegwygeydwdesignatecommence]) {
        [self.view show:@"Please enter the correct format for the phone number，which must start with 9, 09, 8 or 08"];
        return;
    }
    self.mobile = phone.content;
    if ([self.mobile hasPrefix:@"0"]) {
        self.mobile = [self.mobile substringFromIndex:1];
    }
    [dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
    __weak typeof(self) weakSelf = self;
    NSDictionary *params = @{@"mobile": self.mobile, @"pgid": @"1"};
    [[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwlambbreast path:checkNewMobileExsitApi isEncrypt:NO withParams:params success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
        [dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
        __strong typeof(weakSelf) strongSelf = weakSelf;
        dfhgwhruebvhrheysternideology *model = [dfhgwhruebvhrheysternideology yy_modelWithJSON:json];
        if (model.code == 202) {
            dfhgwhruebvhrheyindependenceyearlyViewController *confirmVC = [[dfhgwhruebvhrheyindependenceyearlyViewController alloc] init];
            confirmVC.mobile = strongSelf.mobile;
            confirmVC.isNeedSysMobile = strongSelf.isFromLogin;
            [strongSelf.navigationController pushViewController:confirmVC animated:YES];
        }else {
            [dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
        }
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
    }];
}
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        make.left.bottom.right.equalTo(self.view);
    }];
    [phoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(24);
        make.top.equalTo(bgView).offset(24);
        make.left.equalTo(bgView).offset(13);
    }];
    [phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(56);
        make.centerX.equalTo(bgView);
        make.left.equalTo(bgView).offset(13);
        make.top.equalTo(phoneText.mas_bottom).offset(12);
    }];
    [continueBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phone);
        make.height.mas_equalTo(56);
        make.centerX.equalTo(bgView);
        make.top.equalTo(phone.mas_bottom).offset(40);
    }];
}
@end
